# 使用官方 Node.js 镜像作为构建环境
FROM node:18 AS build

WORKDIR /app

# 复制 package.json 和 package-lock.json
COPY package*.json ./

# 安装依赖
RUN npm install

# 复制项目文件
COPY . .

RUN npm run test:unit

# 构建前端应用
RUN npm run build

# 使用 Nginx 作为生产环境服务器
FROM nginx:alpine as dev-envs

# 删除默认配置
# RUN rm /etc/nginx/conf.d/default.conf

# 复制自定义 Nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf

# 复制构建后的前端文件到 Nginx 目录
COPY --from=build /app/dist /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]